package com.medusa.aps.business.modules.demand.mp.entity;

import cn.hutool.core.annotation.Alias;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.annotation.*;
import com.medusa.aps.business.model.BaseEntity;
import com.medusa.aps.business.model.enums.LabelStatus;
import com.medusa.aps.business.model.enums.ManufacturingStrategy;
import com.medusa.aps.business.model.enums.SchedulingStatusEnum;
import com.medusa.aps.business.model.enums.SourceEnum;
import com.medusa.aps.business.common.mp.IFastJson2TypeHandler;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

/**
 * 订单信息实体类
 *
 * @author yuxh
 * @since 2023-08-14 17:37:47
 */
@Data
@Accessors(chain = true)
@TableName(autoResultMap = true)
@EqualsAndHashCode(callSuper = true)
@Schema(description = "订单信息实体类")
public class OrderInformation extends BaseEntity<OrderInformation> {
    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    @Schema(description = "主键")
    private Integer id;

    /**
     * 标签集合
     */
    @TableField(value = "labels", typeHandler = LabelsTypeHandler.class)
    private Set<LabelStatus> labels;

    /**
     * 订单号
     */
    @Schema(description = "订单号")
    private String orderNumber;

    /**
     * 物料id
     */
    @Schema(description = "物料id")
    private Long materialId;

    /**
     * 物料编码
     */
    @Alias("物料编码")
    @Schema(description = "物料编码")
    private String itemCode;

    /**
     * 物料名称
     */
    @Alias("物料名称")
    @Schema(description = "物料名称")
    private String itemName;

    /**
     * 物料实际库存
     */
    @Alias("物料实际库存")
    @Schema(description = "物料实际库存")
    private BigDecimal itemStock;
    /**
     * 制造策略
     */
    @Alias("制造策略")
    @Schema(description = "制造策略")
    private ManufacturingStrategy manufacturingStrategy;
    /**
     * 客户id
     */
    @Schema(description = "客户id")
    private Integer customerId;
    /**
     * 客户编码
     */
    @Alias("客户编码")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    @Schema(description = "客户编码")
    private String customerCode;

    /**
     * 客户公司名称
     */
    @Alias("客户公司名称")
    @Schema(description = "客户公司名称")
    @TableField(updateStrategy = FieldStrategy.IGNORED)
    private String customerCompanyName;

    /**
     * 下单日期
     */
    @Alias("下单日期")
    @Schema(description = "下单日期")
    private LocalDate orderDate;

    /**
     * 订单交期
     */
    @Alias("订单交期")
    @Schema(description = "订单交期")
    private LocalDate orderDeliveryDate;

    /**
     * 产品数量
     */
    @Alias("产品数量")
    @Schema(description = "产品数量")
    private BigDecimal orderQuantity;

    /**
     * 计划数量
     */
    @Alias("计划数量")
    @Schema(description = "计划数量")
    private BigDecimal plannedQuantity;

    /**
     * 订单状态
     */
    @Alias("订单状态")
    @Schema(description = "订单状态")
    private SchedulingStatusEnum schedulingStatus;

    /**
     * 计划开始时间
     */
    @Alias("计划开始时间")
    @Schema(description = "计划开始时间")
    @TableField(updateStrategy = FieldStrategy.ALWAYS)
    private LocalDateTime plannedStartTime;
    /**
     * 计划结束时间
     */
    @Alias("计划结束时间")
    @Schema(description = "计划结束时间")
    @TableField(updateStrategy = FieldStrategy.ALWAYS)
    private LocalDateTime plannedEndTime;


    /**
     * 订单优先级
     */
    @Alias("订单优先级")
    @Schema(description = "订单优先级")
    private Integer orderPriority;
    /**
     * 计划延期
     */
    @Schema(description = "计划延期")
    @TableField(updateStrategy = FieldStrategy.ALWAYS)
    private Integer planExtension;
    /**
     * 实际延期
     */
    @Schema(description = "实际延期")
    private Integer realityExtension;
    /**
     * 排程备注
     */
    @Schema(description = "排程备注")
    private Boolean isWaitArrange;

    /**
     * 创建人
     */
    @Schema(description = "创建人")
    @TableField(value = "creator", fill = FieldFill.INSERT)
    private String creator;

    /**
     * 数据来源
     */
    @Schema(description = "数据来源")
    private SourceEnum dataSource;

    /**
     * 移出指定标签
     */
    public Set<LabelStatus> removeLabelsValue(LabelStatus labelStatus) {
        Set<LabelStatus> labels = this.labels;
        if (CollUtil.isNotEmpty(labels)) {
            labels.remove(labelStatus);
        } else {
            labels = new HashSet<>();
        }
        return labels;
    }

    /**
     * 获取主键值
     *
     * @return 主键值
     */
    @Override
    public Serializable pkVal() {
        return this.id;
    }

    public static class LabelsTypeHandler extends IFastJson2TypeHandler {

        @Override
        protected TypeReference<Set<LabelStatus>> getTypeReference() {
            return new TypeReference<>() {
            };
        }
    }
}

